using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace ImmunityBuster._2D
{/*
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    /// 

    
    public interface ITexture2D : IEnumerable<Texture2FrameWork>
    { 
    
    }
    public class RBC2D : Microsoft.Xna.Framework.GameComponent
    {
        IEnumerable<Texture2FrameWork> collection ;
        //IEnumerator<Texture2FrameWork> System.Collections.Generic.IEnumerable<Texture2FrameWork>.GetEnumerator();
        
        public ImmunityBuster._2D.Texture2FrameWork _RBC;
        public ImmunityBuster._2D.Texture2FrameWork _RBC2;
        public ImmunityBuster._2D.Texture2FrameWork _RBC3;
        Animation RBC1;
        Animation RBC2;
        Animation RBC3;

        public RBC2D(Game game) : base(game)
        {
            collection = null;
            
            _RBC = new Texture2FrameWork(game);
            RBC1 = new Animation();

            _RBC.Initialize();
        }

        private void loadContent()
        {
            
            _RBC.Texture = Game.Content.Load<Texture2D>("NEW\\RBC");
            
        }


        public void AddRBC()
        {

            loadContent();
        }
        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            _RBC.Position = new Vector2(new Random().Next(550, 600), new Random().Next(100, 330));
            _RBC.rotation = 0;
           


            Texture2D temp;
            temp = Game.Content.Load<Texture2D>("vfx\\RBC");

            RBC1.Initialize(temp, new Vector2(), 150, 150, 8, 10, Color.White, 0.5f, true);
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            _RBC.Rectanlge = new Rectangle((int)_RBC.Position.X, (int)_RBC.Position.Y, _RBC.Texture.Bounds.Width, _RBC.Texture.Bounds.Height);
            
            if (_RBC.Position.X > -20)
                _RBC.Position.X--;
            else
            {
                _RBC.Position.X = new Random().Next(700, 900);
                _RBC.Position.Y = new Random().Next(100, 330);
            }           

            RBC1.Position = _RBC.Position;

            RBC1.Update(gameTime);

            base.Update(gameTime);
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            //spriteBatch.Draw(_RBC.Texture, _RBC.Position, Color.White);
            //spriteBatch.Draw(_RBC2.Texture, _RBC2.Position, Color.White);
            //spriteBatch.Draw(_RBC3.Texture, _RBC3            .Position, Color.White);
            spriteBatch.Draw(_RBC.Texture, _RBC.Position, Color.White);

            RBC1.Draw(spriteBatch, Color.White);
        }
    }
    /*
    class RBC2D
    {
        public interface IRBC
        {
            void Update(GameTime gameTime);
            void Draw(SpriteBatch spriteBatch);
            Rectangle getRectangle();
        }

        class RBC : IRBC
        {
            Texture2FrameWork temp;
            Animation anim;

            public RBC(Game game)
            {
                temp = new Texture2FrameWork(game);
                temp.Texture = game.Content.Load<Texture2D>("vfx\\RBC");
                temp.Position = new Vector2(new Random().Next(700, 1800), new Random().Next(100, 330));
                anim = new Animation();
                anim.Initialize(temp.Texture, temp.Position, 150, 150, 8, 122, Color.White, 0.5f, true);
            }


            public void Update(GameTime gameTime)
            {
                if (anim.Position.X > 0)
                    anim.Position.X--;
                else
                {
                    anim.Position.X = new Random().Next(1000, 2000);
                    anim.Position.Y = new Random().Next(100, 330);
                }
            }
            public Rectangle getRectangle()
            {
                return new Rectangle((int)anim.Position.X,(int)anim.Position.Y, anim.FrameWidth, anim.FrameHeight);
            }

            public void Draw(SpriteBatch spriteBatch)
            {
                anim.Draw(spriteBatch, Color.White);            
            }
        }

        int Count = 3;
        public List<IRBC> RBCs = new List<IRBC>();

        public RBC2D(Game game)
        {
            for (int i = 0; i <= Count; i++)
            {
                RBC rbc = new RBC(game);
                RBCs.Add(rbc);
            }
        
        }


        public void Initialize()
        { 
        
        }

        public void Update(GameTime gameTime)
        {
            foreach (IRBC rbc in RBCs)
            {
                rbc.Update(gameTime);
            }
        
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            foreach (IRBC rbc in RBCs)
            {
                rbc.Draw(spriteBatch);
            }
        
        }
    }*/


    class RBC2D
    {
        public List<RBCBase> EnemyList = new List<RBCBase>();
        Texture2D enemyTexture;
        Random random = new Random();
        static int temp1, temp2;

        public RBC2D(ContentManager Content) 
        {
            enemyTexture = Content.Load<Texture2D>("vfx\\RBC");
            temp1 = new Random().Next(800, 1600);
            temp2 = new Random().Next(150, 400);
            AddEnemy();
            AddEnemy();
            AddEnemy();
            AddEnemy();
        }
        public void Update(GameTimerEventArgs gameTime, Rectangle VecPos)
        {
            UpdateRBCs(gameTime, VecPos);
        }

        private void AddEnemy()
        {
            // Create the animation object
            Animation enemyAnimation = new Animation();
            
            // Initialize the animation with the correct animation information
            enemyAnimation.Initialize(enemyTexture, Vector2.Zero, 255, 255, 8, 100, Color.White, 0.28f, true);


            // Randomly generate the position of the enemy
            int new1 = new Random().Next(800, 1600), new2 = new Random().Next(150, 400);
            while (new1 == temp1)
            {
                new1 = new Random().Next(800, 16600);
            }

            while(new2 == temp2)
            {
                new2 = new Random().Next(150, 400);
            }

            Vector2 position = new Vector2(new1, new2);

            temp1 = new1;
            temp2 = new2;

            // Create an enemy
            RBCBase enemy = new RBCBase();

            // Initialize the enemy
            enemy.Initialize(enemyAnimation, position);

            // Add the enemy to the active enemies list
            EnemyList.Add(enemy);
        }


        void UpdateRBCs(GameTimerEventArgs gameTime, Rectangle VirusPos)
        {
            if (EnemyList.Count == 0)
            {
                while (EnemyList.Count <= 4)
                {
                    AddEnemy();
                }
            }

            for (int i = EnemyList.Count - 1; i >= 0; i--)
            {
                EnemyList[i].Update(gameTime);                
                if (EnemyList[i].Active == false)
                {
                    EnemyList.RemoveAt(i);
                }
            }

            foreach (RBCBase rbc in EnemyList)
            {
                if (VirusPos.Contains(rbc.EnemyAnimation.destinationRect))
                {
                    rbc.Active = false;                
                }
            }

        }

        public void Draw(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < EnemyList.Count; i++)
            {
                EnemyList.ElementAt(i).Draw(spriteBatch);
                
            }
        }
    }

    class RBCBase
    {
        // Animation representing the enemy
        public ImmunityBuster.Animation EnemyAnimation;

        // The position of the enemy ship relative to the top left corner of thescreen
        public Vector2 Position;

        // The state of the Enemy Ship
        public bool Active;

        // The hit points of the enemy, if this goes to zero the enemy dies
        public int Health;

        // The amount of damage the enemy inflicts on the player ship
        public int Damage;

        // The amount of score the enemy will give to the player
        public int Value;

        // Get the width of the enemy ship
        public int Width
        {
            get { return EnemyAnimation.FrameWidth; }
        }

        // Get the height of the enemy ship
        public int Height
        {
            get { return EnemyAnimation.FrameHeight; }
        }

        // The speed at which the enemy moves
        float enemyMoveSpeed;


        public void Initialize(Animation animation, Vector2 position)
        {
            // Load the enemy ship texture
            EnemyAnimation = animation;

            // Set the position of the enemy
            Position = position;

            // We initialize the enemy to be active so it will be update in the game
            Active = true;

            // Set the health of the enemy
            Health = 10;

            // Set the amount of damage the enemy can do
            Damage = 10;

            // Set how fast the enemy moves
            enemyMoveSpeed = 1f;


            // Set the score value of the enemy
            Value = 100;

        }


        public void Update(GameTimerEventArgs gameTime)
        {
            // The enemy always moves to the left so decrement it's xposition
            Position.X -= enemyMoveSpeed;

            if (Position.X < 0)
            {
                Position.X = new Random().Next(700, 2500);
                Position.Y = new Random().Next(150, 400);
            }

            // Update the position of the Animation
            EnemyAnimation.Position = Position;

            // Update Animation
            EnemyAnimation.Update(gameTime);

            // If the enemy is past the screen or its health reaches 0 then deactivateit
            if (Position.X < -Width || Health <= 0)
            {
                // By setting the Active flag to false, the game will remove this objet fromthe 
                // active game list
                Active = false;
            }
        }


        public void Draw(SpriteBatch spriteBatch)
        {
            // Draw the animation
            EnemyAnimation.Draw(spriteBatch, Color.White);
        }

    }
}
